using System;
using KJFramework.Dynamic.Contracts;
using KJFramework.IO.Helper;
using KJFramework.Logger;
using KJFramework.Platform.Client.ProtocolStack;
using KJFramework.Platform.Deploy.DSC.ProtocolStack;

namespace KJFramework.Platform.Deploy.DSC.CP.Connector.Contracts
{
    internal class ComponentTunnelService : IComponentTunnelContract
    {
        #region Implementation of IComponentTunnelContract

        public void Transport(object obj)
        {
            DSCMessage message = (DSCMessage) obj;
            if (message is DSCGetComponentHealthRequestMessage)
            {
                #region Get component health request message

                try
                {
                    DSCGetComponentHealthRequestMessage requestMessage = (DSCGetComponentHealthRequestMessage)message;
                    requestMessage.Bind();
                    foreach (Guid channelId in DynamicServices.GetServiceOnMachine(requestMessage.ServiceName, requestMessage.MachineName))
                    {
                        Global.CommnicationNode.Send(channelId, requestMessage.Body);
                    }
                    return;
                }
                catch (System.Exception ex)
                {
                    Logs.Logger.Log(ex);
                    return;
                }

                #endregion
            }
            if (message is DSCGetFileInfomationRequestMessage)
            {
                #region Get file infomation request message

                try
                {
                    DSCGetFileInfomationRequestMessage requestMessage = (DSCGetFileInfomationRequestMessage)message;
                    requestMessage.Bind();
                    foreach (Guid channelId in DynamicServices.GetServiceOnMachine(requestMessage.ServiceName, requestMessage.MachineName))
                    {
                        Global.CommnicationNode.Send(channelId, requestMessage.Body);
                    }
                    return;
                }
                catch (System.Exception ex)
                {
                    Logs.Logger.Log(ex);
                    return;
                }

                #endregion
            }
            if (message is DSCGetServicesRequestMessage)
            {
                #region Get services request message

                try
                {
                    DSCGetServicesRequestMessage requestMessage = (DSCGetServicesRequestMessage)message;
                    requestMessage.Bind();
                    foreach (Guid channelId in DynamicServices.GetServiceOnMachine(requestMessage.MachineName, requestMessage.ServiceName))
                    {
                        Global.CommnicationNode.Send(channelId, requestMessage.Body);
                    }
                    return;
                }
                catch (System.Exception ex)
                {
                    Logs.Logger.Log(ex);
                    return;
                }

                #endregion
            }
            if (message is DSCResetHeartBeatTimeRequestMessage)
            {
                #region Reset heartbeat time request message

                try
                {
                    DSCResetHeartBeatTimeRequestMessage requestMessage = (DSCResetHeartBeatTimeRequestMessage)message;
                    requestMessage.Bind();
                    foreach (Guid channelId in DynamicServices.GetServiceOnMachine(requestMessage.MachineName, requestMessage.Target))
                    {
                        Global.CommnicationNode.Send(channelId, requestMessage.Body);
                    }
                    return;
                }
                catch (System.Exception ex)
                {
                    Logs.Logger.Log(ex);
                    return;
                }

                #endregion
            }
            if (message is DSCUpdateComponentRequestMessage)
            {
                #region Update component request message

                try
                {
                    DSCUpdateComponentRequestMessage requestMessage = (DSCUpdateComponentRequestMessage)message;
                    requestMessage.Bind();
                    foreach (Guid channelId in DynamicServices.GetServiceOnMachine(requestMessage.ServiceName, requestMessage.MachineName))
                    {
                        Global.CommnicationNode.Send(channelId, requestMessage.Body);
                    }
                    return;
                }
                catch (System.Exception ex)
                {
                    Logs.Logger.Log(ex);
                    return;
                }

                #endregion
            }
            if (message is DSCGetDeployNodesRequestMessage)
            {
                #region Get deploy nodes request message

                #region Notify to client

                try
                {
                    IComponentTunnelContract contract = Global.ComponentInstance.GetTunnel("DSC.Components.ClientProxy");
                    if (contract == null)
                    {
                        ConsoleHelper.PrintLine("Can not transport a object to component tunnel, beacuse this tunnel is null. #name: DSC.Components.ClientProxy.", ConsoleColor.DarkRed);
                        return;
                    }
                    ClientGetDeployNodesResponseMessage clientMessage = new ClientGetDeployNodesResponseMessage();
                    clientMessage.Header.ClientTag = message.Header.ClientTag;
                    clientMessage.Header.TaskId = message.Header.TaskId;
                    clientMessage.Items = DynamicServices.GetDeployNodes();
                    contract.Transport(clientMessage);
                }
                catch (System.Exception ex)
                {
                    Logs.Logger.Log(ex);
                    ConsoleHelper.PrintLine("Can not transport a object to component tunnel. Error message: \r\n" + ex.Message, ConsoleColor.DarkRed);
                }

                #endregion

                #endregion
            }
            if (message is DSCGetCoreServiceRequestMessage)
            {
                #region Get core service request message

                #region Notify to client

                DSCGetCoreServiceRequestMessage msg = (DSCGetCoreServiceRequestMessage) message;
                try
                {
                    IComponentTunnelContract contract = Global.ComponentInstance.GetTunnel("DSC.Components.ClientProxy");
                    if (contract == null)
                    {
                        ConsoleHelper.PrintLine("Can not transport a object to component tunnel, beacuse this tunnel is null. #name: DSC.Components.ClientProxy.", ConsoleColor.DarkRed);
                        return;
                    }
                    ClientGetCoreServiceResponseMessage clientMessage = new ClientGetCoreServiceResponseMessage();
                    clientMessage.Header.ClientTag = message.Header.ClientTag;
                    clientMessage.Header.TaskId = message.Header.TaskId;
                    clientMessage.Items = DynamicServices.GetCoreService(msg.Category);
                    contract.Transport(clientMessage);
                }
                catch (System.Exception ex)
                {
                    Logs.Logger.Log(ex);
                    ConsoleHelper.PrintLine("Can not transport a object to component tunnel. Error message: \r\n" + ex.Message, ConsoleColor.DarkRed);
                }

                #endregion

                #endregion
            }
        }

        #endregion
    }
}